package com.han.jdbc.mybatis._03;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.han.jdbc.mybatis._03.mappers.DeptMapper;
import com.han.jdbc.mybatis._03.mappers.EmpMapperResultMap;
import com.han.jdbc.mybatis.bean.Dept;
import com.han.jdbc.mybatis.bean.Emp;

/**
* @author hanpeng
* @version 创建时间：2020年7月19日 上午10:06:05
* @ClassName 类名称
* @Description 类描述
*/
public class TestResultMap {
	@Test
	public void testGetEmpByEId() {
		String resource="03mybatis-config-resultMap.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapperResultMap mapper = sqlSession.getMapper(EmpMapperResultMap.class);
			Emp emp = mapper.getEmpByEId(1);
			System.out.println(emp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void testGetEmpAndDeptById() {
		String resource="03mybatis-config-resultMap.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapperResultMap mapper = sqlSession.getMapper(EmpMapperResultMap.class);
			Emp emp = mapper.getEmpAndDeptById(1);
			System.out.println(emp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void testGetDeptById() {
		String resource="03mybatis-config-resultMap.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
			Dept dept = mapper.getDeptById(1);
			System.out.println(dept);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void testGetEmpAndDeptByIdSteps() {
		String resource="03mybatis-config-resultMap.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapperResultMap mapper = sqlSession.getMapper(EmpMapperResultMap.class);
			Emp emp = mapper.getEmpAndDeptByIdSteps(1);
			System.out.println("dfsdf");
			System.out.println(emp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void testGetDeptAndEmpsById() {
		String resource="03mybatis-config-resultMap.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
			Dept dept = mapper.getDeptAndEmpsById(1);
			System.out.println("dfsdf");
			System.out.println(dept);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void testGetDeptByIdSteps() {
		String resource="03mybatis-config-resultMap.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
			Dept dept = mapper.getDeptByIdSteps(1);
			System.out.println("dfsdf");
			System.out.println(dept);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	public static void main(String[] args) {
		String resource="03mybatis-config-resultMap.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapperResultMap mapper = sqlSession.getMapper(EmpMapperResultMap.class);
			List<Emp> emps = mapper.getEmpsByDId(1);
			System.out.println("dfsdf");
			System.out.println(emps.size());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
}	
